//测试控制器
package com.cuijian.demo.controller;

import com.cuijian.demo.config.RocketMQConfig;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.cuijian.demo.common.JsonResult;
import com.cuijian.demo.service.RocketMQProducerService;
import com.cuijian.demo.mq.TopicManager;
import org.apache.rocketmq.client.producer.SendResult;

import java.util.HashMap;
import java.util.Map;

@Path("/api/mq")
public class MQTestController extends Controller {

    private RocketMQProducerService producerService = new RocketMQProducerService();

    /**
     * 检查MQ状态
     */
    public void mqStatus() {
        try {
            RocketMQConfig config = RocketMQConfig.getInstance();

            Map<String, Object> status = new HashMap<>();
            status.put("enabled", config.isMqEnabled());
            status.put("nameServer", config.getNameServer());
            status.put("producerGroup", config.getProducerGroup());
            status.put("consumerGroup", config.getConsumerGroup());
            status.put("producerRunning", config.getProducerInstance() != null);
            status.put("consumerRunning", config.getConsumerInstance() != null);
            status.put("statusText", config.getMqStatus().replace("\n", ", "));

            renderSuccess("MQ状态查询成功", status);

        } catch (Exception e) {
            renderError(Integer.parseInt("查询MQ状态失败: " + e.getMessage()));
        }
    }

    private void renderSuccess(String mq状态查询成功, Map<String, Object> status) {
    }

    /**
     * 手动创建Topic
     */
    public void createTopics() {
        try {
            TopicManager.initAllTopics();
            renderJson(JsonResult.success("Topic创建命令已发送").toMap());
        } catch (Exception e) {
            renderJson(JsonResult.error("创建Topic失败: " + e.getMessage()).toMap());
        }
    }

    /**
     * 测试发送订单消息
     */
    public void testOrderMessage() {
        try {
            SendResult result = producerService.sendOrderStatusChangedMessage(1L, "TEST123456", 0, 1);

            if (result != null) {
                renderJson(JsonResult.success("测试消息发送成功: " + result.getMsgId()).toMap());
            } else {
                renderJson(JsonResult.success("消息进入降级处理").toMap());
            }
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(JsonResult.error("测试消息发送失败: " + e.getMessage()).toMap());
        }
    }

    /**
     * 测试发送用户消息
     */
    public void testUserMessage() {
        try {
            SendResult result = producerService.sendUserRegisterMessage(100L, "testuser", "test@example.com");

            if (result != null) {
                renderJson(JsonResult.success("用户测试消息发送成功: " + result.getMsgId()).toMap());
            } else {
                renderJson(JsonResult.success("用户消息进入降级处理").toMap());
            }
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(JsonResult.error("用户测试消息发送失败: " + e.getMessage()).toMap());
        }
    }

    /**
     * 测试发送通知消息
     */
    public void testNotification() {
        try {
            SendResult result = producerService.sendNotificationMessage(
                    "user@example.com",
                    "测试通知",
                    "这是一条测试通知消息",
                    "email"
            );

            if (result != null) {
                renderJson(JsonResult.success("通知测试消息发送成功: " + result.getMsgId()).toMap());
            } else {
                renderJson(JsonResult.success("通知消息进入降级处理").toMap());
            }
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(JsonResult.error("通知测试消息发送失败: " + e.getMessage()).toMap());
        }
    }
}